﻿using FarPoint.Win.Spread;
using GrapeCity.Spreadsheet;
using GrapeCity.Spreadsheet.Drawing;
using Spread.Common.Features.Sample;
using System;
using System.Reflection;
using System.Resources;

namespace Spread.CellTypes.Features
{
    public partial class HyperlinkCells : BaseSampleControl
    {
        public HyperlinkCells()
        {
            InitializeComponent();

            // initialize fpSpread
            fpSpread.AllowCellOverflow = true; // let cells overflow like Excel
            fpSpread.Features.AutoCreateHyperlink = true; // automatically create hyperlinks with separate undo action
            fpSpread.Features.EnhancedShapeEngine = true; // enable enhanced shapes
            fpSpread.Features.ExcelCompatibleKeyboardShortcuts = true; // enable new Excel compatible keyboard shortcuts, allows Ctrl+K for Edit Hyperlink dialog
            // initialize worksheets
            InitializeFormulas();
            InitializeBasicUsage();
            splitContainer1.SplitterDistance = 21; // set split to minimum
        }

        private void InitializeBasicUsage()
        {
            IWorksheet ws = fpSpread.AsWorkbook().Worksheets[0];
            IStyles styles = ws.Workbook.Styles; // workbook styles
            ws.Name = "Basic Usage";
            // initialize cells
            ws.Cells[0, 0, ws.RowCount - 1, ws.ColumnCount - 1].Font.Name = "Calibri";
            ws.Cells[0, 0, ws.RowCount - 1, ws.ColumnCount - 1].Font.Size = 11;
            ws.Cells["B2"].Text = "Hyperlinks basic usage";
            ws.Cells["B2"].Style = styles["title"];
            ws.Cells["B4"].Text = "Cell Reference Link";
            ws.Cells["B4"].Style = styles["exampleTitle"];
            ws.Hyperlinks.Add("B5", "", "Formulas!C6", "click here to go to Formulas!C6\r\nClick and hold to select this cell.", "goto Formulas!C6");
            ws.Tables.Add("B4:B5", YesNoGuess.Yes);
            ws.Cells["D4"].Text = "Name Reference Link";
            ws.Cells["D4"].Style = styles["exampleTitle"];
            ws.Workbook.Names.Add("FormulaExample1", "Formulas!$C$6");
            ws.Hyperlinks.Add("D5", "", "FormulaExample1", "click here to go to FormulaExample1\r\nClick and hold to select this cell.", "goto FormulaExample1");
            ws.Tables.Add("D4:D5", YesNoGuess.Yes);
            ws.Cells["F4"].Text = "Table Reference Link";
            ws.Cells["F4"].Style = styles["exampleTitle"];
            ws.Hyperlinks.Add("F5", "", "Table1", "click here to go to Table1", "goto Table1");
            ws.Tables.Add("F4:F5", YesNoGuess.Yes);
            ws.Cells["B7"].Text = "Email Link";
            ws.Cells["B7"].Style = styles["exampleTitle"];
            ws.Hyperlinks.Add("B8", "mailto:spread.support@mescius.com?subject=spread.support", "", "click here to mail for spread support\r\nClick and hold to select this cell.", "Spread support");
            ws.Tables.Add("B7:B8", YesNoGuess.Yes);
            ws.Cells["B10"].Text = "Web Link";
            ws.Cells["B10"].Style = styles["exampleTitle"];
            ws.Hyperlinks.Add("B11", "http://developer.mescius.com/", "", "click here to go to MESCIUS website\r\nClick and hold to select this cell.", "MESCIUS website");
            ws.Tables.Add("B10:B11", YesNoGuess.Yes);
            ws.Cells["B13"].Text = "Shape Link";
            ws.Cells["B13"].Style = styles["exampleTitle"];
            IShape btn = ws.Shapes.AddShapeToCell(GrapeCity.Spreadsheet.Drawing.AutoShapeType.ReturnButton, 13, 1, ws.Columns[1].ColumnWidth, ws.Rows[13].RowHeight * 2);
            ws.Hyperlinks.Add(btn, "", "Formulas!C10", "click here to go to Formulas!C10\r\nRight-click to select this shape.");
            ws.Cells["B14:B15"].Merge();
            ws.Cells["D13"].Text = "Image Link";
            ws.Cells["D13"].Style = styles["exampleTitle"];
            IShape img = ws.Shapes.AddPictureToCell("Image/Spread_Eagle_small.png", false, true, 13, 3, 60, 45);
            ws.Hyperlinks.Add(img, "", "Formulas!C14", "click here to go to Formulas!C14\r\nRight-click to select this iamge.");
            ws.Columns["A:F"].AutoFit();
            ws.Cells["B17"].Text = "Press Ctrl+K to open the Edit Hyperlink dialog\r\nto edit the link in the active cell or selected shape.";
            ws.Cells["B17"].Style = ws.Workbook.Styles.Add("note", "Note");
        }

        private void InitializeFormulas()
        {
            IWorksheet ws = fpSpread.AsWorkbook().Worksheets.Add("Formulas");
            IStyles styles = ws.Workbook.Styles; // workbook styles
            // create styles for cells
            IStyle titleStyle = styles.Add("title");
            titleStyle.IncludeFont = true;
            titleStyle.Font.Size = 14;
            titleStyle.Font.Bold = true;
            IStyle exampleTitleStyle = styles.Add("exampleTitle");
            exampleTitleStyle.Font.Bold = true;
            exampleTitleStyle.IncludeFont = true;
            IStyle formulaLabelStyle = styles.Add("formulaLabel");
            formulaLabelStyle.Interior.Color = GrapeCity.Spreadsheet.Color.FromArgb(255, 242, 204);
            formulaLabelStyle.IncludePatterns = true;
            IStyle resultLabelStyle = styles.Add("resultLabel");
            resultLabelStyle.Interior.Color = GrapeCity.Spreadsheet.Color.FromArgb(226, 240, 217);
            resultLabelStyle.IncludePatterns = true;
            IStyle formulaExampleStyle = styles.Add("formulaExample");
            formulaExampleStyle.Font.Color = GrapeCity.Spreadsheet.Color.FromArgb(192, 0, 0);
            formulaExampleStyle.IncludePatterns = true;
            // initialize cells
            ws.Cells[0, 0, ws.RowCount - 1, ws.ColumnCount - 1].Font.Name = "Calibri";
            ws.Cells[0, 0, ws.RowCount - 1, ws.ColumnCount - 1].Font.Size = 11;
            ws.Cells["B2"].Text = "HYPERLINK function usage";
            ws.Cells["B2"].Style = styles["title"];
            ws.Cells["B4"].Text = "Cell Reference Link";
            ws.Cells["B4"].Style = styles["exampleTitle"];
            ws.Cells["B5"].Text = "Formula";
            ws.Cells["B5"].Style = formulaLabelStyle;
            ws.Cells["C5"].Formula = "FORMULATEXT(C6)";
            ws.Cells["C5"].Style = styles["formulaExample"];
            ws.Cells["B6"].Text = "Result";
            ws.Cells["B6"].Style = resultLabelStyle;
            ws.Cells["C6"].Formula = "HYPERLINK(\"#'Basic Usage'!B5\",\"goto Basic Usage!B5\")";
            ws.Cells["B8"].Text = "Name Reference Link";
            ws.Cells["B8"].Style = styles["exampleTitle"];
            ws.Cells["B9"].Text = "Formula";
            ws.Cells["B9"].Style = formulaLabelStyle;
            ws.Cells["C9"].Formula = "FORMULATEXT(C10)";
            ws.Cells["C9"].Style = formulaExampleStyle;
            ws.Cells["B10"].Text = "Result";
            ws.Cells["B10"].Style = resultLabelStyle;
            ws.Cells["C10"].Formula = "HYPERLINK(\"#FormulaExample1\",\"goto FormulaExample1\")";
            ws.Cells["B12"].Text = "Table Reference Link";
            ws.Cells["B12"].Style = styles["exampleTitle"];
            ws.Cells["B13"].Text = "Formula";
            ws.Cells["B13"].Style = formulaLabelStyle;
            ws.Cells["C13"].Formula = "FORMULATEXT(C14)";
            ws.Cells["C13"].Style = formulaExampleStyle;
            ws.Cells["B14"].Text = "Result";
            ws.Cells["B14"].Style = resultLabelStyle;
            ws.Cells["C14"].Formula = "HYPERLINK(\"#Table3\",\"goto Table3\")";
            ws.Cells["B16"].Text = "Email Link";
            ws.Cells["B16"].Style = styles["exampleTitle"];
            ws.Cells["B17"].Text = "Formula";
            ws.Cells["B17"].Style = formulaLabelStyle;
            ws.Cells["C17"].Formula = "FORMULATEXT(C18)";
            ws.Cells["C17"].Style = formulaExampleStyle;
            ws.Cells["B18"].Text = "Result";
            ws.Cells["B18"].Style = resultLabelStyle;
            ws.Cells["C18"].Formula = "HYPERLINK(\"mailto:spread.support@mescius.com?subject=spread.support\",\"Spread support\")";
            ws.Cells["B20"].Text = "Web Link";
            ws.Cells["B20"].Style = styles["exampleTitle"];
            ws.Cells["B21"].Text = "Formula";
            ws.Cells["B21"].Style = formulaLabelStyle;
            ws.Cells["C21"].Formula = "FORMULATEXT(C22)";
            ws.Cells["C21"].Style = formulaExampleStyle;
            ws.Cells["B22"].Text = "Result";
            ws.Cells["B22"].Style = resultLabelStyle;
            ws.Cells["C22"].Formula = "HYPERLINK(\"http://developer.mescius.com/\",\"MESCIUS website\")";
            ws.Columns["A:A"].AutoFit();
        }

        private void InsertHyperlinkToolStripMenuItemClick(object sender, EventArgs e)
        {
            HyperlinkForm form = new HyperlinkForm(fpSpread.AsWorkbook().ActiveSheet);
            form.ShowDialog();
        }
    }
}


//
//HyperlinkCells.Designer.cs
//


using FarPoint.Win.Spread;
using System;

namespace Spread.CellTypes.Features
{
    partial class HyperlinkCells
    {
        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(HyperlinkCells));
            this.menuStrip = new System.Windows.Forms.MenuStrip();
            this.insertHyperlinkToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.splitContainer1 = new System.Windows.Forms.SplitContainer();
            this.splitContainer2 = new System.Windows.Forms.SplitContainer();
            this.nameBox = new FarPoint.Win.Spread.NameBox();
            this.fpSpread = new FarPoint.Win.Spread.FpSpread(FarPoint.Win.Spread.LegacyBehaviors.None, ((object)(resources.GetObject("splitContainer2.Panel1.Controls"))));
            this.fpSpread_Sheet1 = this.fpSpread.GetSheet(0);
            this.panel = new System.Windows.Forms.Panel();
            this.formulaTextBox = new FarPoint.Win.Spread.FormulaTextBox();
            this.menuStrip.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
            this.splitContainer1.Panel1.SuspendLayout();
            this.splitContainer1.Panel2.SuspendLayout();
            this.splitContainer1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.splitContainer2)).BeginInit();
            this.splitContainer2.Panel1.SuspendLayout();
            this.splitContainer2.Panel2.SuspendLayout();
            this.splitContainer2.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread)).BeginInit();
            this.panel.SuspendLayout();
            this.SuspendLayout();
            // 
            // menuStrip
            // 
            this.menuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.insertHyperlinkToolStripMenuItem});
            this.menuStrip.Location = new System.Drawing.Point(0, 0);
            this.menuStrip.Name = "menuStrip";
            this.menuStrip.Size = new System.Drawing.Size(635, 24);
            this.menuStrip.TabIndex = 5;
            this.menuStrip.Text = "menuStrip";
            // 
            // insertHyperlinkToolStripMenuItem
            // 
            this.insertHyperlinkToolStripMenuItem.Name = "insertHyperlinkToolStripMenuItem";
            this.insertHyperlinkToolStripMenuItem.Size = new System.Drawing.Size(93, 20);
            this.insertHyperlinkToolStripMenuItem.Text = "&Edit Hyperlink";
            this.insertHyperlinkToolStripMenuItem.Click += new System.EventHandler(this.InsertHyperlinkToolStripMenuItemClick);
            // 
            // splitContainer1
            // 
            this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.splitContainer1.Location = new System.Drawing.Point(0, 24);
            this.splitContainer1.Name = "splitContainer1";
            this.splitContainer1.Orientation = System.Windows.Forms.Orientation.Horizontal;
            // 
            // splitContainer1.Panel1
            // 
            this.splitContainer1.Panel1.Controls.Add(this.splitContainer2);
            this.splitContainer1.Panel1MinSize = 21;
            // 
            // splitContainer1.Panel2
            // 
            this.splitContainer1.Panel2.Controls.Add(this.fpSpread);
            this.splitContainer1.Size = new System.Drawing.Size(635, 463);
            this.splitContainer1.SplitterDistance = 25;
            this.splitContainer1.TabIndex = 4;
            // 
            // splitContainer2
            // 
            this.splitContainer2.Dock = System.Windows.Forms.DockStyle.Fill;
            this.splitContainer2.Location = new System.Drawing.Point(0, 0);
            this.splitContainer2.Name = "splitContainer2";
            // 
            // splitContainer2.Panel1
            // 
            this.splitContainer2.Panel1.Controls.Add(this.nameBox);
            // 
            // splitContainer2.Panel2
            // 
            this.splitContainer2.Panel2.Controls.Add(this.panel);
            this.splitContainer2.Size = new System.Drawing.Size(635, 25);
            this.splitContainer2.SplitterDistance = 211;
            this.splitContainer2.TabIndex = 2;
            // 
            // nameBox
            // 
            this.nameBox.Dock = System.Windows.Forms.DockStyle.Fill;
            this.nameBox.Location = new System.Drawing.Point(0, 0);
            this.nameBox.MaxLength = 255;
            this.nameBox.Name = "nameBox";
            this.nameBox.Size = new System.Drawing.Size(211, 21);
            this.nameBox.TabIndex = 1;
            // Attach nameBox to fpSpread
            this.nameBox.Attach(this.fpSpread);
            // 
            // fpSpread
            // 
            this.fpSpread.AccessibleDescription = "fpSpread, Sheet1, Row 0, Column 0";
            this.fpSpread.Dock = System.Windows.Forms.DockStyle.Fill;
            this.fpSpread.Font = new System.Drawing.Font("Calibri", 11F);
            this.fpSpread.Location = new System.Drawing.Point(0, 0);
            this.fpSpread.Name = "fpSpread";
            this.fpSpread.Size = new System.Drawing.Size(635, 434);
            this.fpSpread.TabIndex = 0;
            // 
            // panel
            // 
            this.panel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.panel.Controls.Add(this.formulaTextBox);
            this.panel.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panel.Location = new System.Drawing.Point(0, 0);
            this.panel.Name = "panel";
            this.panel.Size = new System.Drawing.Size(420, 25);
            this.panel.TabIndex = 0;
            // 
            // formulaTextBox
            // 
            this.formulaTextBox.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.formulaTextBox.Dock = System.Windows.Forms.DockStyle.Fill;
            this.formulaTextBox.Location = new System.Drawing.Point(0, 0);
            this.formulaTextBox.Name = "formulaTextBox";
            this.formulaTextBox.ReadOnly = true;
            this.formulaTextBox.Size = new System.Drawing.Size(418, 23);
            this.formulaTextBox.TabIndex = 3;
            // Attach formulaTextBox to fpSpread
            this.formulaTextBox.Attach(this.fpSpread);
            // 
            // HyperlinkCells
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Controls.Add(this.splitContainer1);
            this.Controls.Add(this.menuStrip);
            this.Name = "HyperlinkCells";
            this.Controls.SetChildIndex(this.menuStrip, 0);
            this.Controls.SetChildIndex(this.splitContainer1, 0);
            this.menuStrip.ResumeLayout(false);
            this.menuStrip.PerformLayout();
            this.splitContainer1.Panel1.ResumeLayout(false);
            this.splitContainer1.Panel2.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit();
            this.splitContainer1.ResumeLayout(false);
            this.splitContainer2.Panel1.ResumeLayout(false);
            this.splitContainer2.Panel2.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.splitContainer2)).EndInit();
            this.splitContainer2.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread)).EndInit();
            this.panel.ResumeLayout(false);
            this.ResumeLayout(false);
            this.PerformLayout();

        }
        #endregion

        private System.Windows.Forms.MenuStrip menuStrip;
        private System.Windows.Forms.ToolStripMenuItem insertHyperlinkToolStripMenuItem;
        private System.Windows.Forms.SplitContainer splitContainer1;
        private System.Windows.Forms.SplitContainer splitContainer2;
        private FarPoint.Win.Spread.NameBox nameBox;
        private FarPoint.Win.Spread.FpSpread fpSpread;
        private System.Windows.Forms.Panel panel;
        private FarPoint.Win.Spread.FormulaTextBox formulaTextBox;
        private SheetView fpSpread_Sheet1;
    }
}
